package com.clj.blesample.http.retrofit;


import com.clj.blesample.BuildConfig;
import com.clj.blesample.entity.AppConstant;
import com.clj.blesample.http.ApiService;
import com.clj.blesample.http.interceptor.LogInterceptor;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RetrofitManager {
    private final Map<String, Object> params = new HashMap<>();
    private static volatile RetrofitManager retrofitManager;
    private OkHttpClient okHttpClient;

    private RetrofitManager() {
    }

    public static RetrofitManager getInstance() {
        if (retrofitManager == null) {
            synchronized (RetrofitManager.class) {
                if (retrofitManager == null) {
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    private Retrofit getRetrofit(String baseUrl) {
        if (!params.containsKey(baseUrl)) {
            params.put(baseUrl, new Retrofit.Builder().baseUrl(baseUrl)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(getOkHttpClient())
                    .build());
        }
        return (Retrofit) params.get(baseUrl);
    }
    public synchronized <T> T getApiService(String baseUrl, Class<T> clazz) {
        String key = clazz.getName() + baseUrl;
        if (!params.containsKey(key)) {
            params.put(key, getRetrofit(baseUrl).create(clazz));
        }
        return (T) params.get(key);
    }


    public static ApiService getApi() {
        return getInstance().getApiService(AppConstant.getUrl(), ApiService.class);
    }

//    private <T> Object getProxyService(String baseUrl, Class<T> clazz) {
//        return Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz}, getProxyHandler(baseUrl, clazz));
//    }
//
//    private <T> InvocationHandler getProxyHandler(String baseUrl, Class<T> clazz) {
//        T service = getRetrofit(baseUrl).create(clazz);
//        return new ProxyHandler(service);
//    }

    //    public OkHttpClient getOkHttpClient(String baseUrl) {
//        return getOkHttpClient();
//    }
    public OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .retryOnConnectionFailure(true)
                    //添加默认header和参数信息
                    .callTimeout(30000, TimeUnit.MILLISECONDS)
                    .writeTimeout(30000, TimeUnit.MILLISECONDS)
                    .connectTimeout(30000, TimeUnit.MILLISECONDS)
                    .readTimeout(30000, TimeUnit.MILLISECONDS);
            if (BuildConfig.DEBUG) {
                //打印日志
                builder.addInterceptor(new LogInterceptor());
            }
            okHttpClient = builder.build();
        }
        return okHttpClient;
    }
}
